<?php
class Db
{
	private $link = null;
	private static $instance = null;

	private function __construct($_db)
	{
		$this->link = mysql_connect('localhost:3308', 'root', 'chengdashun');
		mysql_select_db($_db, $this->link);
		return $this->link;
	}

	private function __clone()
	{
	}

	public static function get_instance($_db)
	{
		if (null === self::$instance) {
			self::$instance = new Db($_db);
		}
		return self::$instance;
	}

	public function rows($_sql)
	{
		$result = mysql_query($_sql, $this->link);
		$rows = array();
		while (false !== $row = mysql_fetch_assoc($result)) {

			array_push($rows, $row);
		}
		return $rows;
	}

	public function row($_sql)
	{
		$rows = $this->rows($_sql);
		return empty($rows) ? null : current($rows);
	}

	public function one($_sql)
	{
		$row = $this->row($_sql);
		return empty($row) ? null : current($row);
	}
}

function pr($_arr)
{
	echo '<pre>';
	print_r($_arr);
	echo '</pre>';
}
$action = $_POST['action'];
$db = Db::get_instance('world');
switch ($action) {
	case 'total':
		$sql = 'SELECT * FROM city';
		$rows = $db->rows($sql);
		echo count($rows);
		break;
	case 'pagination':
		$page = $_POST['p'];
		$limit = 10;
		$start = $page * $limit;
		$sql = 'SELECT * FROM city LIMIT ' . $start . ',' . $limit;
		echo json_encode($db->rows($sql));
		break;
}